package com.example.communist_app_api.controller.web;

import com.example.communist_app_api.db.dto.UserDTO;
import com.example.communist_app_api.db.pojoDto.Role;
import com.example.communist_app_api.db.pojoDto.User;
import com.example.communist_app_api.service.RoleService;
import com.example.communist_app_api.service.UserRoleService;
import com.example.communist_app_api.service.UserService;
import com.example.communist_app_api.util.CosUtils;
import com.example.communist_app_api.util.R;
import com.example.communist_app_api.vo.web.req.*;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author love~mlm
 * @version 1.0
 * Create by 2022/12/28 17:23
 */
@RestController
@RequestMapping("/permission/user")
@Api("用户controller控制器")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserRoleService userRoleService;

    /*
        用户管理
     */

    //查看党支部成员
    @PostMapping("/selectAllUser")
    @ApiOperation("查看所有党员")
    public R selectAllUser(@RequestBody UserSelectReq req) {
        //带条件查询，用户姓名
        PageInfo<UserDTO> pageInfo = userService.findPage(req);

        return R.ok("查询成功").put("pageInfo", pageInfo);
    }

    /*
        显示要修改的用户信息
     */
    @PostMapping("/showUserMessage")
    @ApiOperation("显示要修改的用户信息，回显")
    public R showUserMessage(@RequestBody UserShowUserMessageReq req) {
        Integer userId = req.getUserId();
        //判断用户是否存在
        User user = userService.getUserByUserId(userId);
        if (user == null) {
            return R.error("用户不存在");
        }
        //设置userId,状态,电话号码,姓名,邮箱,并没有头像
        UserDTO userDTO = new UserDTO();
        userDTO.setId(user.getId());
        userDTO.setStatus(user.getStatus());
        userDTO.setTelephone(user.getTelephone());
        userDTO.setName(user.getName());
        userDTO.setEmail(user.getEmail());
        return R.ok().put("userDTO", userDTO);
    }

    /*
        修改用户信息,并没有改头像
     */
    @PostMapping("/updateUserMessage")
    @ApiOperation("修改用户信息")
    public R updateUserMessage(@RequestBody UserUpdateMessageReq req) {
        Integer userId = req.getUserId();
        //判断用户是否存在
        User user = userService.getUserByUserId(userId);
        if (user == null) {
            return R.error("用户不存在");
        }
        //判断req中的Name是否在数据库中重复,确保姓名唯一性
        int equalNum = userService.getUserByUserName(req.getName());
        if (equalNum>=2) {
            return R.error("用户名重复");
        }
        Boolean flag = userService.updateUserMessage(req);
        if (flag == true) {
            return R.ok("修改成功");
        } else {
            return R.error("修改失败");
        }
    }

    /*
        删除用户
     */
    @PostMapping("/deleteUserMessage")
    @ApiOperation("删除用户")
    public R deleteUserMessage(@RequestBody UserDeleteUserMessageReq req) {
        Integer userId = req.getUserId();
        //判断用户是否存在
        User user = userService.getUserByUserId(userId);
        if (user == null) {
            return R.error("用户不存在");
        }
        userService.deleteUserMessage(userId);
        User userOff = userService.getUserByUserId(userId);
        if (userOff == null) {
            return R.ok("删除成功");
        } else {
            return R.error("删除失败");
        }
    }
    /*
        上传头像
     */

    @PostMapping("/uploadUserPhoto")
    @ApiOperation("上传照片")
    public R uploadUserPhoto(@RequestParam("userId") Integer userId ,@RequestBody MultipartFile multipartFile) {
        //判断用户是否存在
        User user = userService.getUserByUserId(userId);
        if (user == null) {
            return R.error("用户不存在");
        }
        //MultipartFile multipartFile = BASE64DecodedMultipartFile.base64ToMultipart(photoBase64);
        //获取头像Url
        String photoUrl = CosUtils.uploadFile(multipartFile, "");
        User updateUser = new User();
        updateUser.setId(userId);
        updateUser.setPhoto(photoUrl);
        userService.editPersonMessage(updateUser);
        User updatedUser = userService.getUserByUserId(userId);
        if (updatedUser.getPhoto().equals(photoUrl)) {
            return R.ok("上传成功");
        } else {
            return R.ok("上传失败");
        }
    }

    /*
    显示所有角色
     */
    @PostMapping("/showAllRole")
    @ApiOperation("显示所有角色")
    public R showAllRole(@RequestBody UserShowAllRoleReq req) {
        Integer userId = req.getUserId();
        List<Role> checkedRoleList = userRoleService.getRoleByUserId(userId);
        List<Role> roleList = roleService.getAllRole();
        return R.ok("查询所有角色成功").put("roleList", roleList).put("checkedRoleList",checkedRoleList);
    }

    /*
        分配角色
     */
    @PostMapping("/assignRole")
    @ApiOperation("分配角色")
    public R assignRole(@RequestBody UserAssignRoleReq req) {
        Integer roleId = req.getRoleId();
        Integer userId = req.getUserId();
        int insertNum = userRoleService.assignRole(userId, roleId);
        if (insertNum == 1) {
            return R.ok("分配成功");
        } else {
            return R.error("分配失败");
        }
    }
}
